package com.houweishi.module.syTableRecord.service.impl;

import com.houweishi.common.utils.DateUtils;
import com.houweishi.module.syTableRecord.domain.SyTableRecordT;
import com.houweishi.module.syTableRecord.mapper.SyTableRecordTMapper;
import com.houweishi.module.syTableRecord.service.ISyTableRecordTService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 记录名Service业务层处理
 * 
 * @author jindy
 * @date 2023-05-24
 */
@Service
public class SyTableRecordTServiceImpl implements ISyTableRecordTService
{
    private static final Logger LOGGER = LoggerFactory.getLogger(SyTableRecordTServiceImpl.class);
    @Autowired
    private SyTableRecordTMapper syTableRecordTMapper;

    /**
     * 查询记录名
     * 
     * @param id 记录名主键
     * @return 记录名
     */
    @Override
    public SyTableRecordT selectSyTableRecordTById(Long id)
    {
        return syTableRecordTMapper.selectSyTableRecordTById(id);
    }

    /**
     * 业务状态
     * @param serialNum
     * @param businessStatus
     */
    @Override
    public void updateBusinessStatus(String serialNum, String businessStatus) {
        try {
            SyTableRecordT syTableRecordT = syTableRecordTMapper.selectSyTableRecordTBySerialNum(serialNum);
            if (Objects.nonNull(syTableRecordT)){
                syTableRecordTMapper.updateBusinessStatus(serialNum,businessStatus,syTableRecordT.getTableName().trim());
            }
        } catch (Exception e) {
            LOGGER.error("修改业务表业务状态失败",e);
        }
    }

    /**
     * 查询记录名列表
     * 
     * @param syTableRecordT 记录名
     * @return 记录名
     */
    @Override
    public List<SyTableRecordT> selectSyTableRecordTList(SyTableRecordT syTableRecordT)
    {
        return syTableRecordTMapper.selectSyTableRecordTList(syTableRecordT);
    }

    /**
     * 新增记录名
     * 
     * @param syTableRecordT 记录名
     * @return 结果
     */
    @Override
    public int insertSyTableRecordT(SyTableRecordT syTableRecordT)
    {
        syTableRecordT.setCreateTime(DateUtils.getNowDate());
        return syTableRecordTMapper.insertSyTableRecordT(syTableRecordT);
    }

    /**
     * 修改记录名
     * 
     * @param syTableRecordT 记录名
     * @return 结果
     */
    @Override
    public int updateSyTableRecordT(SyTableRecordT syTableRecordT)
    {
        syTableRecordT.setUpdateTime(DateUtils.getNowDate());
        return syTableRecordTMapper.updateSyTableRecordT(syTableRecordT);
    }

    /**
     * 批量删除记录名
     * 
     * @param ids 需要删除的记录名主键
     * @return 结果
     */
    @Override
    public int deleteSyTableRecordTByIds(Long[] ids)
    {
        return syTableRecordTMapper.deleteSyTableRecordTByIds(ids);
    }

    /**
     * 删除记录名信息
     * 
     * @param id 记录名主键
     * @return 结果
     */
    @Override
    public int deleteSyTableRecordTById(Long id)
    {
        return syTableRecordTMapper.deleteSyTableRecordTById(id);
    }

    /**
     * 保存表名，流水单号
     * @param entity
     * @param serialNum
     */
    @Override
    public  <T> void saveEntityAndTable(T entity, String serialNum) {
        SyTableRecordT syTableRecordT = new SyTableRecordT();
        syTableRecordT.setTableName(humpToLine(entity.getClass().getSimpleName()).substring(1));
        syTableRecordT.setSerialNum(serialNum);
        syTableRecordT.setCreateTime(DateUtils.getNowDate());
        syTableRecordTMapper.insertSyTableRecordT(syTableRecordT);
    }

    /** 驼峰转下划线 */
    private String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
